<template>
  <div class="padding-middle">
    <el-card>
      <el-form ref="ruleForm" :model="postForm" :rules="rules" style="width: 500px" label-width="80px" label-position="right" class="demo-ruleForm" auto-complete="off">
        <el-form-item label="用户名" prop="userId">
          <el-select v-model="postForm.userId" placeholder="请选择用户名" style="width: 415px">
            <el-option
              v-for="item in useroptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="课程分类" prop="examType" auto-complete="off">
          <el-select v-model="postForm.examType" placeholder="请选择课程分类" style="width: 415px">
            <el-option
              v-for="(item,index) in options"
              :key="index"
              :label="item"
              :value="item"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>import axios from 'axios'
import { types } from '@/assets/type.js'
export default {
  components: { },
  data() {
    return {
      postForm: {
        examType: '',
        userId: ''
      },
      rules: {
        examType: [
          { required: true, message: '请选择考试类型', trigger: 'change' }
        ],
        userId: [
          { required: true, message: '请选择用户名', trigger: 'blur' }
        ]
      },
      useroptions: [],
      options: types
    }
  },
  mounted() {
    if (this.$route.params.id !== undefined && this.$route.params.type !== undefined) {
      this.postForm.userId = this.$route.params.id
      this.postForm.examType = this.$route.params.type
      this.useroptions = [{ value: this.$route.params.id, label: this.$route.params.username }]
    } else {
      this.postForm.userId = ''
      this.postForm.examType = ''
    }
    this.getUserListXiaLa()
  },
  methods: {
    changeCoures(option) {
      console.log(option)
    },
    getUserListXiaLa() {
      axios.get(`/getUserListXiaLa/${'all'}`).then(res => {
        const arry = []
        if (res.success) {
          res.data.forEach((item) => {
            arry.push({ value: item.id, label: item.username })
          })
          this.useroptions = arry
        }
      })
    },
    cancel() {
      this.$router.push('/user/distributionCourse/list')
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post(`/saveUserCourse/`, this.postForm).then(res => {
            if (res.success) {
              this.$router.push('/user/distributionCourse/list')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>
